// import { get } from 'lodash-es'
import { defineStore } from 'pinia'
import { store } from '@/store'
// import dayjs from 'dayjs'
// import type { RouteRecordRaw } from 'vue-router'

import { useCache } from '@/hooks/web/useCache'
import { resetRouter } from '@/router'
import { useTagsViewStoreWithOut } from './tagsView'
import { usePermissionStoreWithOut } from './permission'
// import { nextTick } from 'vue'
// import { getUserInfoApi } from '@/api/user'
import router from '@/router'
import { Hotel } from '@/api/hotel/types'
import { useAppStoreWithOut } from '@/store/modules/app'
import { updateLoginHotelApi } from '@/api/user'

const { wsCache } = useCache()

const tagsViewStore = useTagsViewStoreWithOut()
const permissionStore = usePermissionStoreWithOut()

interface AppParameter {
  FREEZE_ON_PASSWORD_ERROR: string
  TRANSACTION_PASSWORD_REQUIRED: string
  MEMBER_NAME_REQUIRED: string
  MEMBER_MOBILE_REQUIRED: string
  MEMBER_CARD_NUMBER_CASE: string // 会员号大小写
}

// Session会话相关的全局变量

export interface SessionState {
  hotel_id: string
  user_id: string
  login_name: string
  user_type: string
  hotels: Hotel[]
  permissions: string[]
  view_role: 'admin' | 'hotel'
  card_reader_connected: boolean
  parameters: AppParameter
}

export const useSessionStore = defineStore('session', {
  state: (): SessionState => ({
    user_id: '',
    hotel_id: '',
    login_name: '',
    user_type: '',
    hotels: [],
    permissions: [],
    view_role: wsCache.get('view_role') || 'hotel',
    card_reader_connected: false,
    parameters: {
      TRANSACTION_PASSWORD_REQUIRED: '',
      FREEZE_ON_PASSWORD_ERROR: '',
      MEMBER_NAME_REQUIRED: '',
      MEMBER_MOBILE_REQUIRED: '',
      MEMBER_CARD_NUMBER_CASE: ''
    }
  }),
  getters: {
    getLoginName(): string {
      return this.login_name
    },
    getParameters(): AppParameter {
      return this.parameters
    },
    getViewRole(): string {
      return this.view_role
    },
    getUserType(): string {
      return this.user_type
    },
    getHotels(): Hotel[] {
      return this.hotels
    },
    getHotelId(): string {
      return this.hotel_id
    },
    getPermissions(): string[] {
      return this.permissions
    },
    getCardReaderConnected(): boolean {
      return this.card_reader_connected
    }
  },
  actions: {
    setLoginName(value: string) {
      this.login_name = value
    },
    setParameters(value: AppParameter) {
      this.parameters = value
    },
    setHotels(hotels: Hotel[]) {
      this.hotels = hotels
    },
    setPermissions(permissions: string[]) {
      this.permissions = permissions
    },
    setUserId(user_id: string) {
      this.user_id = user_id
    },
    setUserType(user_type: string) {
      this.user_type = user_type
    },
    async setHotelId(hotel_id: string) {
      this.hotel_id = hotel_id
      this.resetPermissions()
    },
    async updateHotelId(hotel_id: string) {
      const res = await updateLoginHotelApi(hotel_id)
      if (res) {
        wsCache.set('token', res.data.token)
        this.resetPermissions()
      } else {
        this.logout()
      }
    },
    async setViewRole(value: 'admin' | 'hotel') {
      wsCache.set('view_role', value)
      this.view_role = value

      this.resetPermissions()
    },
    setCardReaderConnected(value: boolean) {
      this.card_reader_connected = value
    },
    resetPermissions() {
      // 重置router & 重新获取 Router
      resetRouter()
      permissionStore.setIsAddRouters(false)
      tagsViewStore.delAllViews()

      router.push(`/?timestamp=${new Date().getTime()}`)
    },
    logout() {
      this.resetPermissions()
      wsCache.delete('token')
      const appStore = useAppStoreWithOut()
      wsCache.delete(appStore.getUserInfo)

      router.push(`/login`)
    }
  }
})

export const useSessionStoreWithOut = () => {
  return useSessionStore(store)
}
